package app.client.file.rename;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.lily.com.file.FileUtil;

/**
 * 漫画重命名处理类
 * @author JiaJ
 * @date 2018年1月6日
 *
 */
public class ComicRename {
	
	private static void demo(){
		
		File file = new File("E:/BaiduYunDownload/爱神巧克力/001");
		
		File[] files = file.listFiles();
		
		List<Long> times = new ArrayList<Long>();			
		Map<Long,String> tmpFiles = new HashMap<Long,String>();
		
		for(File child:files){
				
			times.add(child.lastModified());
			tmpFiles.put(child.lastModified(), child.getAbsolutePath().replaceAll("\\\\", "/"));
			
		}
		
		Collections.sort(times);
		
		int index = 1;
		
		for(Long activeTime:times){
			
			FileUtil.renameFile(tmpFiles.get(activeTime), index+".jpg");
			
			index ++;
			
		}
		
	}
	
	public static void rename(int index,String filePath){
		
		if(filePath.endsWith(".webp")){
			FileUtil.renameFile(filePath,index+".jpg");
			return;
		}
		
		int lastIdx = filePath.lastIndexOf(".");
		
		String extName = filePath.substring(lastIdx);
		
		FileUtil.renameFile(filePath,index+extName);
		
	}
	
	
	
	public static void main(String[] args) {
		
		File file = new File("E:/BaiduYunDownload/爱神巧克力（276 - ）");
		
		File[] pFiles = file.listFiles();
		
		for(File pFile:pFiles){
			
			System.out.println(pFile.getAbsolutePath()+"文件处理.....");
			
			File[] cFiles = pFile.listFiles();
			
			List<Long> times = new ArrayList<Long>();			
			Map<Long,String> tmpFiles = new HashMap<Long,String>();
			
			for(File cFile:cFiles){
				
				times.add(cFile.lastModified());
				tmpFiles.put(cFile.lastModified(), cFile.getAbsolutePath().replaceAll("\\\\", "/"));
				
			}
			
			Collections.sort(times);
			
			int index = 1;
			
			for(Long activeTime:times){
				
				rename(index,tmpFiles.get(activeTime));
				
				index ++;
				
			}

		}
		
	}
	
}
